#ifdef __KERNEL__
#include <linux/random.h>
#else
#include <stddef.h>
#include <stdio.h>
#endif
#include "random.h"

#ifndef __KERNEL__
static int read_random_user(unsigned char* r, const size_t len)
{
    size_t nread;
    FILE*  fp = fopen("/dev/urandom", "r");
    if (!fp) return -1;
    nread = fread(r, 1, len, fp);
    fclose(fp);
    if (nread != len) return -2;
    return 0;
}
#endif

int random_generator(unsigned char* r, const int len)
{
#ifdef __KERNEL__
    get_random_bytes(r, len);
    return 0;
#else
    return read_random_user(r, len);
#endif
}
